package com.example.testrecorder;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import com.example.testrecorder.utils.ActivityCollector;

public class BaseActivity extends AppCompatActivity {
    private MyReceiver myReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityCollector.getInstance().addOne(this);//把当前活动放入集合中
        myReceiver=new MyReceiver();
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(myReceiver,new IntentFilter("offline"));
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(myReceiver);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.getInstance().removeOne(this);//把当前活动从集合中移除
    }
    class MyReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            AlertDialog.Builder builder=new AlertDialog.Builder(BaseActivity.this);
            builder.setTitle("提示");
            builder.setCancelable(false);
            builder.setMessage("您的账号在别处登录，被强制下线！");
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    ActivityCollector.getInstance().removeAll();
                    startActivity(new Intent(BaseActivity.this,MainActivity.class));
                }
            });
            builder.show();
        }
    }

}